package org.lansir.af.view;

import java.util.Calendar;

import org.lansir.af.R;
import org.lansir.af.view.adapter.NumericWheelAdapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TimePicker;

/**
 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
 */
public class TimePickerWheel extends AlertDialog implements OnClickListener{


    private static final String HOUR = "hour";
    private static final String MINUTE = "minute";
    

    private final OnTimeWheelSetListener mCallback;
    private final Calendar mCalendar;
    private final java.text.DateFormat mDateFormat;
    
    private int mInitialHourOfDay;
    private int mInitialMinute;

    private WheelView mWheelHours;
    private WheelView mWheelMins;
	
	// Time scrolled flag
	private boolean timeScrolled = false;
    /**
     * @param context Parent.
     * @param callBack How parent is notified.
     * @param hourOfDay The initial hour.
     * @param minute The initial minute.
     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     */
    public TimePickerWheel(Context context,
    		OnTimeWheelSetListener callBack,
            int hourOfDay, int minute) {
        this(context, R.style.NoBgDialog,
                callBack, hourOfDay, minute);
    }

    /**
     * @param context Parent.
     * @param theme the theme to apply to this dialog
     * @param callBack How parent is notified.
     * @param hourOfDay The initial hour.
     * @param minute The initial minute.
     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     */
    public TimePickerWheel(Context context,
            int theme,
            OnTimeWheelSetListener callBack,
            int hourOfDay, int minute) {
        super(context, theme);
        mCallback = callBack;
        mInitialHourOfDay = hourOfDay;
        mInitialMinute = minute;

        mDateFormat = DateFormat.getTimeFormat(context);
        mCalendar = Calendar.getInstance();
        updateTitle(mInitialHourOfDay, mInitialMinute);
        
        setButton(context.getText(R.string.date_picker_wheel_set), this);
        setButton2(context.getText(R.string.date_picker_wheel_cancel), (OnClickListener) null);
        setIcon(R.drawable.ic_title_settings);
        
        LayoutInflater inflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker, null);
        setView(view);
        
        mWheelHours = (WheelView)view.findViewById(R.id.hour);
        mWheelMins = (WheelView)view.findViewById(R.id.minute);
        mWheelHours.setViewAdapter(new NumericWheelAdapter(getContext(), 0, 23));
        mWheelMins.setViewAdapter(new NumericWheelAdapter(getContext(), 0, 59, "%02d"));
        mWheelMins.setCyclic(true);
        
        mWheelHours.setCurrentItem(mInitialHourOfDay);
        mWheelMins.setCurrentItem(mInitialMinute);
        
        OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!timeScrolled) {
					updateTitle(mWheelHours.getCurrentItem(), mWheelMins.getCurrentItem());
				}
			}
		};
		
		mWheelHours.addChangingListener(wheelListener);
		mWheelMins.addChangingListener(wheelListener);
		
		OnWheelClickedListener click = new OnWheelClickedListener() {
            public void onItemClicked(WheelView wheel, int itemIndex) {
                wheel.setCurrentItem(itemIndex, true);
            }
        };
        mWheelHours.addClickingListener(click);
        mWheelMins.addClickingListener(click);
        
        OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				timeScrolled = true;
			}
			public void onScrollingFinished(WheelView wheel) {
				timeScrolled = false;
				updateTitle(mWheelHours.getCurrentItem(), mWheelMins.getCurrentItem());
			}
		};
		
		mWheelHours.addScrollingListener(scrollListener);
		mWheelMins.addScrollingListener(scrollListener);

        // initialize state
  
    }
    
    public void onClick(DialogInterface dialog, int which) {
        if (mCallback != null) {
            mCallback.onTimeWheelChanged(mWheelHours.getCurrentItem(), mWheelMins.getCurrentItem());
        }
    }
    
    public void updateTime(int hourOfDay, int minutOfHour) {
    	mWheelHours.setCurrentItem(hourOfDay);
    	mWheelMins.setCurrentItem(minutOfHour);

    }

    private void updateTitle(int hour, int minute) {
        mCalendar.set(Calendar.HOUR_OF_DAY, hour);
        mCalendar.set(Calendar.MINUTE, minute);
        setTitle(mDateFormat.format(mCalendar.getTime()));
    }
    
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(HOUR, mWheelHours.getCurrentItem());
        state.putInt(MINUTE, mWheelMins.getCurrentItem());
        return state;
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int hour = savedInstanceState.getInt(HOUR);
        int minute = savedInstanceState.getInt(MINUTE);
        mWheelHours.setCurrentItem(hour);
        mWheelMins.setCurrentItem(minute);
        updateTitle(hour, minute);
    }
    
    
    public interface OnTimeWheelSetListener{
    	public void onTimeWheelChanged(int hourOfDay, int minute);
    }
}